package fristphase.submarinegame08complete;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    protected int state = LIVE;//当前状态

    protected int width,height,x,y,speed,index;
    protected String name;

    //潜艇的超类构造方法
    public SeaObject(int width,int height,int index,String name){
        this.width = width;
        this.height = height;
        this.index = index;
        this.name = name;
        x = -width;
        Random random = new Random();
        y = random.nextInt(459-height-150+1)+150;
        speed = random.nextInt(3)+1;
    }

    //战斗船，水雷，炸弹的超类构造函数
    public SeaObject(int width,int height,int x,int y,int speed,String name){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.name = name;
    }

    public void position(){
        System.out.println(name+index+"的坐标是"+x+","+y);
    }

    public abstract void move();

    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state == LIVE;
    }
    public boolean isDead(){
        return  state == DEAD;
    }


    //这块代码不知道啥含义
    //如果是活着的话，就将getImage()所执行的返回结果，
    // 也就是各个游戏对象的图片绘画出来
    public void paintImage(Graphics g){
        if(isLive()){
            this.getImage().paintIcon(null,g,x,y);
        }
    }

    //检测潜艇越界
    public boolean isOutOfBounds(){
        return x>= World.WIDTH;
    }

    //检查碰撞
    public boolean isHit(SeaObject other){//other是子弹类
        int x1 = this.x - other.width;
        int x2 = this.x + this.width;
        int y1 = this.y - other.height;
        int y2 = this.y + this.height;
        return x1<=other.x && x2>=other.x && y1<=other.y && y2>=other.y;
    }

    //状态为dead
    public void goDead(){
        state = DEAD;
    }

}

//得分接口
interface EnemyScore{
    int getScore();
}

//奖励接口
interface EnemyLife{
    int getLife();
}
